<?php

namespace Driver\Controller;

use Driver\Controller\BaseController;

class DriverController extends BaseController {

    public function _initialize() {
        parent::_initialize();
        if (!$this->driver_id) {
            $this->error('请先登录');
        }
    }
    
    public function updateDriverInfo(){
        $driver_id = $this->driver_id;
        $data['fleet_id'] = $this->getParam('fleet_id',0,'int+',TRUE,'请选择车队');
        $data['driver_name'] = $this->getParam('driver_name', '','string',TRUE, '请填写姓名');
        $data['driver_identity'] = $this->getParam('driver_identity');
        $data['driver_linkman'] = $this->getParam('driver_linkman');
        $data['driver_linkman_phone'] = $this->getParam('driver_linkman_phone');
        $data['route_line'] = $this->getParam('route_line');
        $data['car_no'] = $this->getParam('car_no','','string',TRUE,'请填写车牌号');
        $data['car_bridge'] = $this->getParam('car_no',0,'int+');
        $fileData['white_card'] = $this->getParam('white_card');
        $fileData['identity_front'] = $this->getParam('identity_front');
        $fileData['identity_back'] = $this->getParam('identity_back');
        $fileData['driver_card'] = $this->getParam('driver_card');
        $fileData['driving_card'] = $this->getParam('driving_card');
        $fileData['driver_face'] = $this->getParam('driver_face');
        
        $addData = array();
//        if(isset($_FILES['teacher_photo'])){
//            $insertData['teacher_photo'] = $this->uploadfile('teacher_photo','teacher_photo',5*1024*1024);
//        }
        foreach($fileData as $key=>$val){
            if(isset($_POST[$key])){
                $addData[$key] = '';
                if($val){
                    $addData[$key] = $this->uploadFileBase64($val, 'Driver');
                }
            }
        }
        foreach($data as $key => $val){
            if(isset($_POST[$key])){
                $addData[$key] = $val;
            }
        }
        $res = M('driver')->where(['driver_id'=>$driver_id])->save($addData);
        if($res){
            $this->success();
        }else{
            $this->error();
        }
        
    }
    
    //司机信息
    public function driverInfo(){
        $driver_id = $this->driver_id;
        $driverInfo = M('driver')->where(['driver_id'=>$driver_id])->find();
        !empty($driverInfo['driver_face']) && $driverInfo['driver_face'] = C('SITE_URL').$driverInfo['driver_face'];
        $thisMonth = date('Y-m-01 00:00:01', strtotime(date("Y-m-d")));
        $Order = M('Order');
        $monthWhere['driver_id'] = $driver_id;
        $monthWhere['create_time'] = array('EGT',$thisMonth);
        $driverInfo['month_order'] = $Order->where($monthWhere)->count();
        $driverInfo['order_count'] = $Order->where(array('driver_id'=>$driver_id))->count();
        $monthWhere['order_status'] = 50;
        $driverInfo['month_price'] = $Order->where($monthWhere)->getField('sum(driver_price)');
        $driverInfo['month_price'] = empty($driverInfo['month_price']) ? '0.00' : $driverInfo['month_price'];
        $driverInfo['total_price'] = $driverInfo['driver_total_amount'];
        $driverInfo['evel_score'] = '3.0';
        //车队名
        $driverInfo['fleet_name'] = M('fleet')->where(array('fleet_id'=>$driverInfo['fleet_id']))->getField('fleet_name');
        $driverInfo['fleet_name'] = empty($driverInfo['fleet_name']) ? '' : $driverInfo['fleet_name'];
        
        !empty($driverInfo['white_card']) && $driverInfo['white_card'] = C('SITE_URL'). $driverInfo['white_card'];
        !empty($driverInfo['identity_front']) && $driverInfo['identity_front'] = C('SITE_URL'). $driverInfo['identity_front'];
        !empty($driverInfo['identity_back']) && $driverInfo['identity_back'] = C('SITE_URL'). $driverInfo['identity_back'];
        !empty($driverInfo['driver_card']) && $driverInfo['driver_card'] = C('SITE_URL'). $driverInfo['driver_card'];
        !empty($driverInfo['driving_card']) && $driverInfo['driving_card'] = C('SITE_URL'). $driverInfo['driving_card'];
        $this->returnList($driverInfo);
    }
    
    //消息列表
    public function msglist(){
        $where['planfrom'] = 1;
        $where['user_id'] = $this->driver_id;
        $res = M('msg')->where($where)->save(['is_read'=>1]);
        $list = M('msg')->where($where)->order('msg_id DESC')->select();
        $this->returnList($list);
    }
    
    //未读消息
    public function msgNoReadcount(){
        $where['planfrom'] = 1;
        $where['user_id'] = $this->driver_id;
        $where['is_read'] = 0;
        $data['msg_no_read_count'] = M('msg')->where($where)->count();
        $this->returnList($data);
    }
    
    //更新已读
    public function updateMsgRead(){
        $where['msg_id'] = $this->getParam('msg_id',0,'int+',TRUE);
        $where['planfrom'] = 1;
        $res = M('msg')->where($where)->save(['is_read'=>1]);
        $this->success();
    }


    //司机收支明细
    public function walletFlow(){
        $driver_id = $this->driver_id;
        $walletChangeList = M()->table('ot_driver_wallet_change dwc')
                    ->field('dwc.*,o.box_no')
                    ->join('LEFT JOIN ot_order as o ON o.order_id = dwc.order_id')
                    ->where(array('dwc.driver_id' => $driver_id))
                    ->order('wallet_change_id desc')
                    ->select();
        
        $this->returnList($walletChangeList);
    }
    
    //更新司机头像和名称
    public function updateDriverFaceAndName(){
        $driver_id = $this->driver_id;
        $data['driver_name'] = $this->getParam('driver_name', '','string',TRUE, '请填写姓名');
        $fileData['driver_face'] = $this->getParam('driver_face');
        
        $addData = array();
//        if(isset($_FILES['teacher_photo'])){
//            $insertData['teacher_photo'] = $this->uploadfile('teacher_photo','teacher_photo',5*1024*1024);
//        }
        foreach($fileData as $key=>$val){
            if(isset($_POST[$key])){
                $addData[$key] = '';
                if($val){
                    $addData[$key] = $this->uploadFileBase64($val, 'Driver');
                }
            }
        }
        foreach($data as $key => $val){
            if(isset($_POST[$key])){
                $addData[$key] = $val;
            }
        }
        $res = M('driver')->where(['driver_id'=>$driver_id])->save($addData);
        if($res){
            $this->success();
        }else{
            $this->error();
        }
        
    }
    
}
